package com.spit.fammo.ui.frg

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.view.View.OnClickListener
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ConvertUtils
import com.kingja.loadsir.core.LoadService
import com.spit.amoeba.base.BaseFragment
import com.spit.fammo.R
import com.spit.fammo.adapter.StockTakeListAdapter
import com.spit.fammo.api.UIHelper
import com.spit.fammo.databinding.FStockTakeListBinding
import com.spit.fammo.ext.STOCK_TAKE_LIST_PAGE
import com.spit.fammo.ext.init
import com.spit.fammo.ext.loadListData
import com.spit.fammo.ext.loadServiceInit
import com.spit.fammo.ext.setNbOnItemClickListener
import com.spit.fammo.ext.showLoading
import com.spit.fammo.mar.eventViewModel
import com.spit.fammo.viewmodel.AssetModel
import com.spit.fammo.weight.recyclerview.SpaceItemDecoration
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import me.hgj.jetpackmvvm.ext.nav

/**
 * User: Nike
 *  2024/12/13 17:33
 *      盘点列表
 */
class StockTakeListFrg: BaseFragment<AssetModel, FStockTakeListBinding>(), OnClickListener{

    val adapter: StockTakeListAdapter by lazy { StockTakeListAdapter(arrayListOf()) }

    lateinit var loadsir: LoadService<Any>

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.includeSearch.fyAdd.visibility = View.GONE
        mDatabind.includeSearch.fyZking.setOnClickListener(this)
        loadsir = loadServiceInit(mDatabind.swipeRefresh) {
            lazyLoadData()
        }
        mDatabind.swipeRefresh.isEnabled = false
        mDatabind.swipeRefresh.setOnRefreshListener {
            mViewModel.onStockTakeListOffice()
        }
        mDatabind.recyclerView.init(LinearLayoutManager(context), adapter).let {
            it.addItemDecoration(SpaceItemDecoration(ConvertUtils.dp2px(10f), ConvertUtils.dp2px(5f), true))
        }
        adapter.run {
            setNbOnItemClickListener{adapter, view, position ->
                val bean = mFilterList[position]
                UIHelper.startAssetFrg(nav(), bean.stocktakeno, bean.name)
            }
        }
        mDatabind.includeSearch.etInput.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                adapter!!.filter.filter(p0.toString())
                GlobalScope.launch(Dispatchers.Main) {
                    delay(300)
                    setToolBarTitle(getString(R.string.stock_take) + "(" + adapter.mFilterList.size + ")")
                }
            }

            override fun afterTextChanged(p0: Editable?) {
            }
        })
        lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onResume(owner: LifecycleOwner) {
                super.onResume(owner)
                setToolBarTitle(getString(R.string.stock_take) + "(" + adapter.mFilterList.size + ")")
                mViewModel.onStockTakeListOffice()
            }
        })
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.listBean.observe(viewLifecycleOwner, Observer {
            loadListData(it, adapter, loadsir, mDatabind.recyclerView, mDatabind.swipeRefresh, it.pageSize)
            adapter.appendList(it.listData)
            setToolBarTitle(getString(R.string.stock_take) + "(" + adapter.mFilterList.size + ")")
        })

        eventViewModel.zkingType.observeInFragment(this, Observer { dabaBean ->
            if (dabaBean.Type == STOCK_TAKE_LIST_PAGE){
                val bean = adapter.mFilterList.firstOrNull { it.stocktakeno == dabaBean.text }
                if (bean != null) {
                    UIHelper.startAssetFrg(nav(), bean.stocktakeno, bean.name)
                }
            }
        })
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        loadsir.showLoading()
        mViewModel.onStockTakeListOffice()
    }

    override fun onClick(p0: View?) {
        when(p0?.id){
            R.id.fy_zking -> UIHelper.startZxingAct(STOCK_TAKE_LIST_PAGE)
        }
    }

}